package view;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import model.dao.ConectaBanco;
import model.dao.Popular;

public class VBanco extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final int BANCO_MYSQL = 1, ARRAY = 2;
	
	private JPanel contentPane;
	private JComboBox<String> CBoxBanco = new JComboBox<String>();
	private String[] aBancos = {"MYSQL","ARRAY"}; 
	private JTextField tf_Login;
	private JPasswordField pw_Senha;
	private JLabel lblUrl;
	private JLabel lblLogin;
	private JLabel lblSenha;
	private JTextField tf_Url;
	Popular pb = new Popular();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					VBanco frame = new VBanco();
					frame.setVisible(true);
					frame.setLocationRelativeTo(null);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public VBanco() {
		setTitle("Conex\u00E3o com o Banco");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblEscolhaOBanco = new JLabel("Escolha o Banco que Gostaria de Conectar");
		lblEscolhaOBanco.setHorizontalAlignment(SwingConstants.CENTER);
		lblEscolhaOBanco.setFont(new Font("Trebuchet MS", Font.BOLD | Font.ITALIC, 15));
		lblEscolhaOBanco.setBounds(10, 0, 414, 66);
		contentPane.add(lblEscolhaOBanco);
		CBoxBanco.setBounds(105, 77, 215, 23);
		CBoxBanco.addItem("0 - Sem Banco");
		for (int i = 0; i < aBancos.length; i++) {
			CBoxBanco.addItem((1+i)+" - "+aBancos[i]); // preencher o combobox com a lista de bancos.
	
		}
		contentPane.add(CBoxBanco);
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				VBanco.this.dispose();
				System.exit(0);
			}
		});
		btnCancelar.setFont(new Font("Trebuchet MS", Font.BOLD, 12));
		btnCancelar.setBounds(10, 228, 89, 23);
		contentPane.add(btnCancelar);
		
		JButton btnConfirmar = new JButton("Confirmar");
		btnConfirmar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String[] b = CBoxBanco.getSelectedItem().toString().trim()
						.split(" - ");
				int repositorio = Integer.parseInt(b[0]);
				
				switch (repositorio) {
				case BANCO_MYSQL:
					String url 		= tf_Url.getText().trim();
					String username = tf_Login.getText().trim();
					String password = new String(pw_Senha.getPassword());
					
					if (ConectaBanco.CriarConexaoMySQL(url, username, password) != null) {
						if (JOptionPane.showConfirmDialog(null,
								"Gostaria de popular o Banco ?", "Popular",
								JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
							try {
								pb.popularMysql();
							} catch (ClassNotFoundException | SQLException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							

						VBanco.this.setVisible(false);
						VLogin telaLogin = new VLogin(BANCO_MYSQL);
						telaLogin.setVisible(true);

					} else {
						JOptionPane.showMessageDialog(
										null,
										"Verifique se o Banco está ativo ou os parametros (Url, Usuário e Senha)",
										"Erro", JOptionPane.ERROR_MESSAGE);
					}
					
					break;
				case ARRAY:
					
					
					try {
						pb.popularArray();
					} catch (ClassNotFoundException | SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					VBanco.this.setVisible(false);
					VLogin telaLogin = new VLogin(ARRAY);
				
					telaLogin.setVisible(true);
					break;
					
				default:
					JOptionPane.showMessageDialog(null, "Escolha um banco para poder rodar a aplica\u00E7\u00E3o","Informa\u00E7\u00E3o",JOptionPane.INFORMATION_MESSAGE);
					break;
				}
			}
		});
		btnConfirmar.setFont(new Font("Trebuchet MS", Font.BOLD, 12));
		btnConfirmar.setBounds(335, 228, 89, 23);
		contentPane.add(btnConfirmar);
		
		lblUrl = new JLabel("URL:");
		lblUrl.setVisible(false);
		lblUrl.setFont(new Font("Trebuchet MS", Font.BOLD, 11));
		lblUrl.setBounds(10, 128, 46, 14);
		contentPane.add(lblUrl);
		
		lblLogin = new JLabel("Login:");
		lblLogin.setVisible(false);
		lblLogin.setFont(new Font("Trebuchet MS", Font.BOLD, 11));
		lblLogin.setBounds(10, 153, 46, 14);
		contentPane.add(lblLogin);
		
		lblSenha = new JLabel("Senha:");
		lblSenha.setVisible(false);
		lblSenha.setFont(new Font("Trebuchet MS", Font.BOLD, 11));
		lblSenha.setBounds(10, 180, 46, 14);
		contentPane.add(lblSenha);
		
		tf_Url = new JTextField();
		tf_Url.setText("//localhost:3306/Controle_de_chamados");
		tf_Url.setVisible(false);
		tf_Url.setBounds(66, 125, 358, 20);
		contentPane.add(tf_Url);
		tf_Url.setColumns(10);
		
		tf_Login = new JTextField();
		tf_Login.setVisible(false);
		tf_Login.setBounds(66, 150, 141, 20);
		contentPane.add(tf_Login);
		tf_Login.setColumns(10);
		
		pw_Senha = new JPasswordField();
		pw_Senha.setVisible(false);
		pw_Senha.setBounds(66, 177, 141, 20);
		contentPane.add(pw_Senha);
		CBoxBanco.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				String[] b = CBoxBanco.getSelectedItem().toString().trim()
						.split(" - ");
				int banco = Integer.parseInt(b[0]);
				escolhaBanco(banco);
			}
		});

	}
	private void escolhaBanco(int banco){
		if (banco==BANCO_MYSQL){
			tf_Url.setVisible(true);
			tf_Login.setVisible(true);
			pw_Senha.setVisible(true);
			lblLogin.setVisible(true);
			lblSenha.setVisible(true);
			lblUrl.setVisible(true);
		}else{
			tf_Url.setVisible(false);
			tf_Login.setVisible(false);
			pw_Senha.setVisible(false);
			lblLogin.setVisible(false);
			lblSenha.setVisible(false);
			lblUrl.setVisible(false);
		}

	}
}
